package com.zenika.zenbinding.client.validator;

import com.google.gwt.core.client.GWT;
import com.zenika.zenbinding.client.exception.DataMessage;
import com.zenika.zenbinding.client.exception.ValidatorException;

public class MinValidator implements Validator {

	private String min;
	private DataMessage message;

	public MinValidator(String min) {
		this.min = min;
		this.message = new DataMessage("Value entered must be greater than " + min);
	}

	public void validate(Object object) throws ValidatorException {
		String type = GWT.getTypeName(object);
		if (type.equals("java.lang.Integer")) {
			this.validateInteger((Integer) object);
		} else if (type.equals("java.lang.Double")) {
			this.validateDouble((Double) object);
		} else if (type.equals("java.lang.Float")) {
			this.validateFloat((Float) object);
		} else if (type.equals("java.lang.Short")) {
			this.validateShort((Short) object);
		} else if (type.equals("java.lang.Byte")) {
			this.validateByte((Byte) object);
		} else if (type.equals("java.lang.Long")) {
			this.validateLong((Long) object);
		} else {
			throw new ClassCastException("This validator only works for numeric type objects");
		}
	}

	public void validateInteger(Integer value) throws ValidatorException {
		Integer minInteger = new Integer(this.min);
		int compare = minInteger.compareTo(value);
		if (compare > 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateDouble(Double value) throws ValidatorException {
		Double minDouble = new Double(this.min);
		int compare = minDouble.compareTo(value);
		if (compare > 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateFloat(Float value) throws ValidatorException {
		Float minFloat = new Float(this.min);
		int compare = minFloat.compareTo(value);
		if (compare > 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateShort(Short value) throws ValidatorException {
		Short minShort = new Short(this.min);
		int compare = minShort.compareTo(value);
		if (compare > 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateByte(Byte value) throws ValidatorException {
		Byte minByte = new Byte(this.min);
		int compare = minByte.compareTo(value);
		if (compare > 0) {
			throw new ValidatorException(this.message);
		}
	}

	public void validateLong(Long value) throws ValidatorException {
		Long minLong = new Long(this.min);
		int compare = minLong.compareTo(value);
		if (compare > 0) {
			throw new ValidatorException(this.message);
		}
	}

}
